package com.cqupt.sendMes.utils;

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;


public class EncryptConfigUtil {
    public static PooledPBEStringEncryptor buildEncryptorConfig(String SECRET) {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(SECRET);
        config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        return encryptor;
    }

    public static String encrypt(String value, String secret) {
        PooledPBEStringEncryptor encryptor = buildEncryptorConfig(secret);
        String encryptString = encryptor.encrypt(value);
        return "ENC(" + encryptString + ")";
    }

    public static String decrypt(String value, String secret) {
        PooledPBEStringEncryptor encryptor = buildEncryptorConfig(secret);
        value = value.substring(4, value.length() - 1);
        String decryptString = encryptor.decrypt(value);
        return decryptString;
    }

    public static void main(String[] args) {


//        String str = "";
//        String secret = "";
//        System.out.println(encrypt(str, secret));
    }
}
